package com.example.myapplication;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentTransaction;

import com.example.myapplication.fragment.Cate;
import com.example.myapplication.fragment.Home;
import com.example.myapplication.fragment.My;
import com.example.myapplication.fragment.Shop;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.container,new Home());
        transaction.commit();
        BottomNavigationView bnv=findViewById(R.id.tabbar);
        bnv.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                FragmentTransaction transaction2=getSupportFragmentManager().beginTransaction();
                if(item.getItemId()==R.id.home){
                    transaction2.replace(R.id.container,new Home());
                }
                else if(item.getItemId()==R.id.cate){
                    transaction2.replace(R.id.container,new Cate());
                }
                else if(item.getItemId()==R.id.shop){
                    transaction2.replace(R.id.container,new Shop());
                }
                else{
                    transaction2.replace(R.id.container,new My());
                }
                transaction2.commit();
                return true;
            }
        });
    }
}